/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <queue.h>
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

extern UART_HandleTypeDef huart1;
typedef struct info{

	uint8_t which;  //哪个灯
	uint8_t state;  //灯状态

}info_t;


/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
osThreadId_t LEDTaskHandle;
osThreadId_t UARTTaskHandle;
xQueueHandle LEDqueue;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
const osThreadAttr_t LEDTask_attributes = {
  .name = "LEDTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};

const osThreadAttr_t UARTTask_attributes = {
  .name = "UARTTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);
void StartLEDTask(void *arg);
void StartUARTTask(void* arg);
void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */
   LEDqueue=xQueueCreate(1,sizeof(info_t));
	
	
	
  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
  
  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */
  LEDTaskHandle=osThreadNew(StartLEDTask, NULL, &LEDTask_attributes);
	UARTTaskHandle=osThreadNew(StartUARTTask, "helloworld", &UARTTask_attributes);
  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */

  for(;;)
  {
    osDelay(5000);
	
  }
  /* USER CODE END StartDefaultTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void StartLEDTask(void *arg)
{
    info_t st;
		 uint8_t state;
   for(;;)
	 {   
		   xQueueReceive(LEDqueue,&st,portMAX_DELAY);
		   if(st.state=='0')
			 {
			   state=GPIO_PIN_SET;  //灯灭
			 }
			 else if(st.state=='1')
			 {
			    state=GPIO_PIN_RESET;  //灯亮
			   
			 }
		
		 switch(st.which)
		 {
			 case '1':
			   HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,(GPIO_PinState)state);
			   break;
			 case '2':
				 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_2,(GPIO_PinState)state);
			   break;
		   case '3':
				 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_1,(GPIO_PinState)state);
			   break;
		   case '4':
				 HAL_GPIO_WritePin(GPIOB,GPIO_PIN_0,(GPIO_PinState)state);
			   break;

		 }
		

	 }

}
void StartUARTTask(void* arg)
{ info_t data;
	uint8_t ret[2]={0};
  for(;;)
	  {
	
			HAL_UART_Receive(&huart1,ret,2,HAL_MAX_DELAY);
			if(ret[0]>='1'&&ret[0]<='4'&&ret[1]>='0'&&ret[1]<='1')
			{
			  data.which=ret[0];
				data.state=ret[1];
			
			
			}
			
			
      xQueueSend(LEDqueue,&data,portMAX_DELAY);
		}
  
}